package Lambda;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.function.Consumer;

/**
 * Created by xiebang
 * Description
 * User:小谢
 * Date:2021-12-02
 * Time:21:09
 */
//无参数无返回值
@FunctionalInterface
interface  NoParameterNoReturn{
    void test();
}


//无返回值一个参数
@FunctionalInterface
interface OneParameterNoReturn {
    void test(int a);
}
//无返回值多个参数
@FunctionalInterface
interface MoreParameterNoReturn {
    void test(int a,int b);
}
//有返回值无参数
@FunctionalInterface
interface NoParameterReturn {
    int test();
}
//有返回值一个参数
@FunctionalInterface
interface OneParameterReturn {
    int test(int a);
}
//有返回值多参数
@FunctionalInterface
interface MoreParameterReturn {
    int test(int a,int b);
}
class Test{
    public void func(){
        System.out.println("func()");
    }
}
public class TestDemo {
    public static void main11(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("Hello");
        list.add("world");
        list.add("hello");
        list.add("lambda");
        System.out.println(list);
//        list.sort(new Comparator<String>() {
//            @Override
//            public int compare(String o1, String o2) {
//                return o1.compareTo(o2);
//            }
//        });
//        System.out.println(list);
        System.out.println("===========================");
        list.sort((o1,o2) -> {
            return o1.compareTo(o2);
        });
        System.out.println(list);
    }
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("Hello");
        list.add("world");
        list.add("hello");
        list.add("lambda");
        list.forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        });
        System.out.println("===========================");
        list.forEach(s -> System.out.println(s));
    }
    public static void main9(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("Hello");
        list.add("bit");
        list.add("hello");
        list.add("lambda");
//        list.forEach(new Consumer<String>() {
//            @Override
//            public void accept(String s) {
//                System.out.println(s);
//            }
//        });
        System.out.println("===========================");
        list.forEach(s -> System.out.println(s));
        list.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });
        System.out.println("===========================");
        list.sort((String o1,String o2) -> {
            return o1.compareTo(o2);
        });
        PriorityQueue priorityQueue = new PriorityQueue(new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                return 0;
            }
        });
    }
    public static void main8(String[] args) {
        int a = 10;
        NoParameterNoReturn noParameterNoReturn = () -> {
//            a = 99;这会报错 捕获
            System.out.println("haha");
            System.out.println(a);
        };
        noParameterNoReturn.test();
    }
    public static void main7(String[] args) {
        int a = 10;

        new Test(){
            @Override
            public void func(){

                System.out.println("这是一个重写的内部类方法");
                System.out.println("我是一个常量或者是一个没有改变过值的变量" + a);//这里a就是一个被捕获的变量
            }
        }.func();

    }
    public static void main6(String[] args) {
//        MoreParameterReturn moreParameterReturn = (int a,int b) -> a + b;
        MoreParameterReturn moreParameterReturn = (a,b) -> a + b;

        System.out.println(moreParameterReturn.test(10, 20));

    }
    public static void main5(String[] args) {
//        OneParameterReturn oneParameterReturn = (int a)-> a;
        OneParameterReturn oneParameterReturn =  a-> a;
        System.out.println(oneParameterReturn.test(19));

    }
    public static void main4(String[] args) {
//        NoParameterReturn noParameterReturn = ()-> {return 10;};
        NoParameterReturn noParameterReturn = ()->  10;
        System.out.println(noParameterReturn.test());
    }
    public static void main3(String[] args) {
        MoreParameterNoReturn moreParameterNoReturn =
                (int a,int b)-> System.out.println(a+b);
//        MoreParameterNoReturn moreParameterNoReturn =
//                (a,b)-> System.out.println(a+b);//类型可以省略，但是一般不建议省略类型
        moreParameterNoReturn.test(10,20);
    }
    public static void main2(String[] args) {
        //OneParameterNoReturn oneParameterNoReturn = (int a) -> System.out.println(a);
        OneParameterNoReturn oneParameterNoReturn = a -> System.out.println(a);
        oneParameterNoReturn.test(10);
    }
    public static void main1(String[] args) {
        NoParameterNoReturn noParameterNoReturn = new NoParameterNoReturn() {
            @Override
            public void test() {
                System.out.println("哈哈");
            }
        };
        noParameterNoReturn.test();
        System.out.println("=================================");
        NoParameterNoReturn noParameterNoReturn1 = ()->
            System.out.println("haha");
        noParameterNoReturn1.test();
    }
}
